package cn.edu.neusoft.neufooddemo.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.edu.neusoft.neufooddemo.R;
import cn.edu.neusoft.neufooddemo.bean.CommentInfo;
import cn.edu.neusoft.neufooddemo.bean.Result;
import cn.edu.neusoft.neufooddemo.common.Constants;
import cn.edu.neusoft.neufooddemo.listener.RetrofitListener;
import cn.edu.neusoft.neufooddemo.model.FoodModel;

/**
 * Created by Shinelon on 2017/4/6.
 */

public class CommentAdapter extends RecyclerViewAdapter<CommentAdapter.ViewHolder> implements RetrofitListener<Result>{
    private int pos;
    private FoodModel foodModel;
    public CommentAdapter(Context context, List data, int layoutResource) {
        super(context, data, layoutResource);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final CommentInfo entity = (CommentInfo) data.get(position);
        holder.mycomment_itemid.setText(entity.getItem_id());
        holder.mycomment_foodname.setText(entity.getFoodname());
        holder.mycomment_commenttime.setText(entity.getComment_time());
        holder.mycomment_content.setText(entity.getContent());
        holder.mycomment_shopname.setText("【" + entity.getShopname() + "】");

        holder.mycomment_alter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final EditText et = new EditText(context);
                et.setText(entity.getContent());
                new AlertDialog.Builder(context).setTitle("修改评论")
                        .setView(et)
                        .setPositiveButton("确定",new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog, int which) {
                                alterComment(entity.getItem_id(),et.getText().toString());
                            }
                        }).setNegativeButton("取消",null)
                        .show();
            }
        });

        holder.mycomment_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pos=position;
                deleteComment(entity.getItem_id());
            }
        });
    }

   public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView mycomment_foodname, mycomment_commenttime, mycomment_content, mycomment_shopname,mycomment_itemid;
        Button mycomment_alter, mycomment_delete;

        public ViewHolder(View itemView) {
            super(itemView);
            mycomment_itemid = (TextView) itemView.findViewById(R.id.mycomment_itemid);
            mycomment_foodname = (TextView) itemView.findViewById(R.id.mycomment_foodname);
            mycomment_commenttime = (TextView) itemView.findViewById(R.id.mycomment_commenttime);
            mycomment_content = (TextView) itemView.findViewById(R.id.mycomment_content);
            mycomment_shopname = (TextView) itemView.findViewById(R.id.mycomment_shopname);
            mycomment_alter = (Button) itemView.findViewById(R.id.mycomment_alter);
            mycomment_delete = (Button) itemView.findViewById(R.id.mycomment_delete);
        }
    }

    private void alterComment(String item_id,String et){
        foodModel=new FoodModel();
        foodModel.updateComment(item_id,et,this);
    }

    private void deleteComment(String item_id){
        foodModel=new FoodModel();
        foodModel.deleteComment(item_id,this);
    }

    @Override
    public void onSuccess(Result status, int flag) {
        if(flag== Constants.UPDATECOMMENT)
        {
            if(status.getSuccess().equals("1")){
                Toast.makeText(context,"修改成功", Toast.LENGTH_SHORT).show();
                notifyDataSetChanged();
            }else{
                Toast.makeText(context,"修改失败", Toast.LENGTH_SHORT).show();
            }

        }else
        {
            if(status.getSuccess().equals("1")){
                removeData(pos);
                Toast.makeText(context,"删除成功", Toast.LENGTH_SHORT).show();
                notifyDataSetChanged();
            }else{
                Toast.makeText(context,"删除失败", Toast.LENGTH_SHORT).show();
            }

        }
    }

    @Override
    public void onFail() {

    }

    public void removeData(int position) {
        data.remove(position);
        notifyItemRemoved(position);
    }
}
